package edu.yonsei.iwb.sdt.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.graph.layout.TreeGraphLayout;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.widget.Widget;
import edu.yonsei.iwb.sdt.datastructure.tree.DEdge;
import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import edu.yonsei.iwb.sdt.datastructure.tree.NodeType;

public class SDTViewPanel extends JPanel{
    
    private MainFrame mf;
    private Dimension maximumSize;
    
    
    
    public SDTViewPanel(MainFrame mf){
        this.mf = mf;
        this.setBackground(Color.WHITE);
        this.maximumSize = new Dimension();
        this.maximumSize.setSize(200, 100);
        
    }
    
    
    public SDTGraphScene makeGraphInfo(SDTGraphScene scene, DNode dnode, String parentNodeID){        
                
        Vector<DEdge> edgeSet = dnode.getEdge();
        for (int i = 0; i < edgeSet.size(); i++) {
            DEdge de = edgeSet.get(i);
            DNode childnode = de.getChildNode();
            String childNodeID = childnode.toGUIString();
            String edge = de.toGUIString();
            Widget childNodeWidget = scene.addNode(childNodeID);
            childNodeWidget.setToolTipText(childnode.getTooltipTEXT());
                scene.setEdgeColor(Color.BLUE);
                scene.addEdge(edge);                
                scene.setEdgeSource(edge, parentNodeID);
                scene.setEdgeTarget(edge, childNodeID);
                childNodeWidget.setMaximumSize(maximumSize);                
                childNodeWidget.setMinimumSize(maximumSize);                
            if(childnode.NODE_TYPE == NodeType.BRANCHNODE){
                this.makeGraphInfo(scene, childnode, childNodeID);                
            }       
        }
        
//        scene.getActions().addAction(ActionFactory.createZoomAction());
        
        return scene;
    }

    public void showGraph(DNode node) {
        this.removeAll();
        SDTGraphScene scene = new SDTGraphScene();                
        
        String parentNodeID = node.toGUIString();
        Widget parentNodeWidget = scene.addNode(parentNodeID);
        parentNodeWidget.setToolTipText(node.getTooltipTEXT());
        parentNodeWidget.setEnabled(true);
        parentNodeWidget.setOpaque(true);
        parentNodeWidget.setMaximumSize(maximumSize);
        parentNodeWidget.setMinimumSize(maximumSize);
        makeGraphInfo(scene, node,parentNodeID);
        
        TreeGraphLayout tgl = new TreeGraphLayout(scene, 40, 20, 60, 40, true);
        tgl.layout(node.toGUIString());        
        
        JComponent createView = scene.createView();
        
//        int width = 800, height = 600;
        this.add(createView, BorderLayout.CENTER);
        
//        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//        this.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
        
        
        
        this.setVisible(true);
        
//        show(createView);
    }

    public void show(JComponent sceneView) {
        JScrollPane panel = new JScrollPane(sceneView);
        panel.getHorizontalScrollBar().setUnitIncrement(32);
        panel.getHorizontalScrollBar().setBlockIncrement(256);
        panel.getVerticalScrollBar().setUnitIncrement(32);
        panel.getVerticalScrollBar().setBlockIncrement(256);

        showCoreEDT(sceneView);
    }

    private void showCoreEDT(JComponent view) {
        int width = 800, height = 600;
        this.add(view, BorderLayout.CENTER);                
//        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
        this.setVisible(true);
    }

}
